﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;
using LUStationery.Model;
using LUStationery.DAO;
using LUStationery.Controller;

namespace LUStationery.Controller
{
    public class RequisitionFormControl
    {
        public RequisitionFormControl() { }

        public RequisitionForm getRequisitionFormByRequisitionFormID(string requisitionFormID)
        {
            RequisitionFormDAO rfDAO = new RequisitionFormDAO();
            IDataReader idr = rfDAO.getRemarkByRequisitionFormID(requisitionFormID);
            return RequisitionFormControl.convertIdrToRequisitionForm(idr);
        }

        public RequisitionFormList getRequisitionFormsByUserName(string username)
        {
            RequestListControl rlcont = new RequestListControl();
            RequestList rList = rlcont.getRequestsByUserName(username);
            HashSet<string> rfIDs=new HashSet<string>();
            foreach (Request r in rList)
            {
                rfIDs.Add(r.RequisitionFormID);
            }
            RequisitionFormDAO rfDAO=new RequisitionFormDAO();
            IDataReader idr = rfDAO.getRequisitionFormsByIDs(rfIDs);
            return convertIdrToRequisitionFormList(idr);
        }

        public string getRemarkByRequisitionFormID(string requisitionFormID)
        {
            RequisitionFormDAO rfDAO = new RequisitionFormDAO();
            IDataReader idr = rfDAO.getRemarkByRequisitionFormID(requisitionFormID);
            string remark = RequisitionFormControl.convertIdrToRequisitionForm(idr).Remark;
            return remark;
        }



        protected static RequisitionFormList convertIdrToRequisitionFormList(IDataReader idr)
        {
            RequisitionFormList rfList = new RequisitionFormList();
            while (idr.Read())
            {
                RequisitionForm rf = new RequisitionForm();
                rf.RequisitionFormID = Convert.ToString(idr["requisition_form_id"]);
                rf.Remark = Convert.ToString(idr["remark"]);
                rfList.Add(rf);
            }
            return rfList;
        }

        protected static RequisitionForm convertIdrToRequisitionForm(IDataReader idr)
        {
            RequisitionForm rf = new RequisitionForm();
            while (idr.Read())
            {
                rf.RequisitionFormID = Convert.ToString(idr["requisition_form_id"]);
                rf.Remark = Convert.ToString(idr["remark"]);
            }
            return rf;
        }
    }
}
